import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/order/work_order_controller.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';
import 'package:kkhousekeep/widget/kk_default_tab_controller.dart';

import '../../../../models/order_count_entity.dart';
import '../repair/repair_home_controller.dart';

class WorkOrderPage extends StatelessWidget {
  const WorkOrderPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar('报修工单'),
      body: GetBuilder(
        init: WorkOrderController(),
        builder: (worKOrderController) => KKDefaultTabController(
          length: worKOrderController.tabs.length,
          onTabListener: (tabController) {
            int tabId = worKOrderController.tabs[tabController.index].id;
            worKOrderController.performClickTab(tabId: tabId);
          },
          builder: (tabController) => Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              _builderTabBarWidget(worKOrderController, tabController),
              _builderTabBarViewWidget(worKOrderController, tabController),
            ],
          ),
        ),
      ),
    );
  }

  Widget _builderTabBarWidget(
    WorkOrderController worKOrderController,
    TabController tabController,
  ) {
    return Obx(() => TabBar(
          isScrollable: false,
          controller: tabController,
          padding: EdgeInsets.zero,
          labelPadding: EdgeInsets.zero,
          indicator: const BoxDecoration(),
          tabs: worKOrderController.tabs.map((tab) {
            bool isEnable = tab.id == worKOrderController.tabId.value;
            return Tab(
              child: Container(
                width: double.infinity,
                height: 88.h,
                color: Colors.white,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Positioned(
                      child: Text(
                        tab.title,
                        style: TextStyle(
                          fontSize: 32.sp,
                          color: isEnable
                              ? const Color(0xFF1774E0)
                              : const Color(0xFF999999),
                        ),
                      ),
                    ),
                    Positioned(
                      bottom: 2.h,
                      child: Container(
                        width: 48.w,
                        height: 4.h,
                        decoration: BoxDecoration(
                          gradient: isEnable
                              ? const LinearGradient(
                                  colors: [
                                    Color(0xFF0A4EF1),
                                    Color(0xFF7AE0E7),
                                  ],
                                )
                              : null,
                          borderRadius: BorderRadius.circular(2.h),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            );
          }).toList(),
        ));
  }

  Widget _builderTabBarViewWidget(
    WorkOrderController worKOrderController,
    TabController tabController,
  ) {
    return Expanded(
        flex: 1,
        child: Container(
          margin: EdgeInsets.only(top: 16.w),
          child: TabBarView(
              controller: tabController,
              children: worKOrderController.tabs
                  .map((tab) => ListView(
                        cacheExtent: 96.h,
                        children: worKOrderController.tabViewItems.map((model) {
                          return Obx(() => _builderTabBarViewItemWidget(
                                tabId: tab.id,
                                worKOrderController: worKOrderController,
                                model: model,
                              ));
                        }).toList(),
                      ))
                  .toList()),
        ));
  }

  Widget _builderTabBarViewItemWidget({
    required int tabId,
    required WorkOrderController worKOrderController,
    required TabModel model,
  }) {
    bool isSelfView = tabId == worKOrderController.tabs[0].id;
    OrderCountObj entity = isSelfView
        ? worKOrderController.selfCountEntity.value
        : worKOrderController.transferredCountEntity.value;
    OrderState orderState = OrderState.values[model.id];
    bool isLabelVisible = false;
    int count = 0;

    if (OrderState.pending == orderState) {
      count = entity.pending;
    }

    if (OrderState.processing == orderState) {
      count = entity.processing;
    }

    if (OrderState.hanging == orderState) {
      count = entity.hanging;
    }

    if (OrderState.processed == orderState) {
      //交互需要显示已处理的工单，但不需要再显示红点了
      // count = entity.processed;
      count = 0;
    }

    isLabelVisible = count != 0;

    return InkWell(
      onTap: () => worKOrderController.performClick(model),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 32.w),
        margin: EdgeInsets.only(bottom: 2.h),
        height: 96.h,
        color: Colors.white,
        child: Stack(
          alignment: AlignmentDirectional.centerStart,
          children: [
            Positioned(
              left: 0,
              child: Text(
                model.title,
                style: TextStyle(
                  fontSize: 32.sp,
                  color: const Color(0XFF434649),
                ),
              ),
            ),
            Positioned(
              right: 0,
              top: 0,
              bottom: 0,
              child: Icon(
                Icons.navigate_next,
                color: const Color(0XFFCECECE),
                size: 48.w,
              ),
            ),
            Positioned(
              right: 46.w,
              child: Badge(
                backgroundColor: const Color(0xFFDD5045),
                textColor: Colors.white,
                textStyle: TextStyle(
                  fontSize: 20.sp,
                ),
                padding: EdgeInsets.only(left: 10.w, right: 10.w),
                label: Text('$count'),
                largeSize: 32.h,
                isLabelVisible: isLabelVisible,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
